<?php
include('include/config.inc.php');
$msg = 0;
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['sendSMS'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['sendSMS'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$msgGet = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : 0;
	$partyType = isset($_REQUEST['partyType']) ? $_REQUEST['partyType'] : 0;
  $techArray   = array();
  $totalSms    = 0;
  $currentDate = date("Y-m-d");
  $selectTech = "SELECT technicianId,lastName,firstName,userType,mobile1
                   FROM technician";
  $selectTechResult = mysql_query($selectTech);
  $i = 0;
  while($techRow = mysql_fetch_array($selectTechResult))
  {
  	$techArray[$i]['mobile1'] = $techRow['mobile1'];
    $techArray[$i]['technicianId'] = $techRow['technicianId'];
    if($techRow['userType'] == "None")
      $techArray[$i]['techName']  = $techRow['firstName']."".$techRow['lastName'];
    else
      $techArray[$i]['techName']  = $techRow['firstName']." ".$techRow['lastName']."<b>-".$techRow['userType'];
    $i++; 
  }
  if(isset($_POST['techSub']))
  {
  	
    // Send message to Additional numbers
	  if(isset($_POST['numbers']) && $_POST['numbers'] != "")
	  {
			$extraNum = $_POST['numbers'];
			$arr = explode(",", $extraNum);
			foreach($arr as $value)
			{
			  $insertSms = "INSERT INTO smsFacility(uid,mobileNum,message,date)
			                VALUES(".$_SESSION['s_activeUserId'.$cfgPack].",'".$value."','".$_POST['msgText']."','".$currentDate."')";;
			  $insertSmsRes = mysql_query($insertSms);
			}
	    
    $extraNumArr = array();
    $extraNumArr = $_POST['numbers'];
    $newNum      =  str_replace(',', ',91', $extraNumArr);
//    $ID          = "info@edsystemsindia.com";
//    $Pwd         = "password";
//    $baseurl     = "http://sms.megasoftware.in";
    $ID          = "EDSystems";
    $Pwd         = "EDSystems";
    $baseurl     = "http://ip.muicsms.co.in/smsserver";
    $PhNo        = "91".$newNum;
    $Text        = urlencode($_POST['msgText']);
    //Invoke HTTP Submit url
    $url = "$baseurl/SMS10N.aspx?Userid=$ID&UserPassword=$Pwd&PhoneNumber=$PhNo&Text=$Text&GSM=EDSYSM";
//    $url = "$baseurl/sms.aspx?Id=$ID&Pwd=$Pwd&PhNo=$PhNo&text=$Text";
    // do sendmsg call
    $ret = file($url);
    //Process $ret to check whether it contains "Message Submitted"
    //.............											.
    //..............											
	  }
	  
	  // Send message to Selected Technician
	  if(isset($_POST['techCheck']))
	  {
	    $loopCount = 0;
	    $numberArray = array();
	    while($loopCount < count($_POST['techCheck']))
	    {
	    	$techId  = ($_POST['techCheck'][$loopCount] != '') ? $_POST['techCheck'][$loopCount] : 0;
	      if($_POST['techCheck'][$loopCount] != "")
	      {
	        $selectTech = "SELECT technicianId,mobile1
	                         FROM technician
	                        WHERE technicianId=".$techId;
	        $techRes = mysql_query($selectTech);
	        
	        while($techRow = mysql_fetch_array($techRes))
	        {
	          $insertSms = "INSERT INTO smsFacility(technicianId,uid,mobileNum,message,date)
	                        VALUES(".$techId.",".$_SESSION['s_activeUserId'.$cfgPack].",'".$techRow['mobile1']."','".$_POST['msgText']."','".$currentDate."')";
	          $inserSmsRes = mysql_query($insertSms);
//            $ID = "info@edsystemsindia.com";
//            $Pwd = "password";
//            $baseurl ="http://sms.megasoftware.in";
            $PhNo = "91".$techRow ['mobile1'];
            $Text = urlencode($_POST['msgText']);
            $ID          = "EDSystems";
            $Pwd         = "EDSystems";
            $baseurl     = "http://ip.muicsms.co.in/smsserver";
            //Invoke HTTP Submit url
            $url = "$baseurl/SMS10N.aspx?Userid=$ID&UserPassword=$Pwd&PhoneNumber=$PhNo&Text=$Text&GSM=EDSYSM";
//            
//            //Invoke HTTP Submit url
//          $url = "$baseurl/sms.aspx?Id=$ID&Pwd=$Pwd&PhNo=$PhNo&text=$Text";
//            // do sendmsg call
            $ret = file($url);
            //Process $ret to check whether it contains "Message Submitted"
            //..............
            //..............
	        }
	      }
	      $loopCount++;
	    }
	  }
	  // Send message to Selected Technician Over
	  
	  // Send message to Selected Different Party Type : Starts
	  if(isset($_POST['pTypeCheck']))
	  {
	    $loopCount = 0;
	    $numberArray = array();
	    while($loopCount < count($_POST['pTypeCheck']))
	    {
	    	$insPartyId = substr_replace($_REQUEST['pTypeCheck'][$loopCount],"",-1);
	    	
	    	if(substr($_REQUEST['pTypeCheck'][$loopCount],-1) == 'P')
        {
        	$partyId    = $insPartyId;
        	$supplierId = 0;
        	$queryField = "partyId AS mainId, mobile1, mobile2, mobile3";
        	$queryTable = "party";
        	$queryWhere = "partyId = ".$partyId."";
        }
        else
        {
        	$partyId    = 0;
        	$supplierId = $insPartyId;
        	$queryField = "supplierId AS mainId, mobile1, mobile2, 0 AS mobile3";
        	$queryTable = "supplier";
        	$queryWhere = "supplierId = ".$supplierId."";
        }
	      
	      if($insPartyId != "")
	      {
	      	
	        $selectQuery = "SELECT ".$queryField."
	                         FROM ".$queryTable."
	                        WHERE ".$queryWhere;
	        $selectQueryRes = mysql_query($selectQuery);
	        
	        while($row = mysql_fetch_array($selectQueryRes))
	        {
	        	if($row['mobile1'] != "" || $row['mobile1'] != 0)
	        	{
	        		$mobileNum = $row['mobile1'];
	        	}
	        	elseif($row['mobile2'] != "" || $row['mobile2'] != 0)
	        	{
	        		$mobileNum = $row['mobile2'];
	        	}
	        	elseif($row['mobile3'] != "" || $row['mobile3'] != 0)
	        	{
	        		$mobileNum = $row['mobile3'];
	        	}
	        	else
	        	{
	        		$mobileNum = 0;
	        	}
	        	
	          $insertSms = "INSERT INTO smsFacility(uid,partyId, supplierId, mobileNum, message, date)
	                        VALUES(".$_SESSION['s_activeUserId'.$cfgPack].",'".$partyId."','".$supplierId."','".$mobileNum."','".$_POST['msgText']."','".$currentDate."')";
	          $inserSmsRes = mysql_query($insertSms);
//            $ID = "info@edsystemsindia.com";
//            $Pwd = "password";
//            $baseurl ="http://sms.megasoftware.in";
            $PhNo = "91".$techRow ['mobile1'];
            $Text = urlencode($_POST['msgText']);
            $ID          = "EDSystems";
            $Pwd         = "EDSystems";
            $baseurl     = "http://ip.muicsms.co.in/smsserver";
            //Invoke HTTP Submit url
            $url = "$baseurl/SMS10N.aspx?Userid=$ID&UserPassword=$Pwd&PhoneNumber=$PhNo&Text=$Text&GSM=EDSYSM";
//            //Invoke HTTP Submit url
//          $url = "$baseurl/sms.aspx?Id=$ID&Pwd=$Pwd&PhNo=$PhNo&text=$Text";
//            // do sendmsg call
            $ret = file($url);
//            //Process $ret to check whether it contains "Message Submitted"
//            //..............
            //..............
            if($inserSmsRes)
            {
              header("location:sendSMS.php?msg=1");	
            }
	        }
	      }
	      $loopCount++;
	    }
	  }
	  // Send message to Selected Different Party Type : Ends
	}
	$smsCount = "SELECT count(smsId) AS totalSms
	               FROM smsFacility";
	$smsRes = mysql_query($smsCount);
  while($smsRow = mysql_fetch_array($smsRes))
  {
    $totalSms = $smsRow['totalSms'];
  }
  if($totalSms >= $cfgSmsCount)
  {
//    $ID = "info@edsystemsindia.com";
//    $Pwd = "password";
//    $baseurl ="http://sms.megasoftware.in";
    $PhNo = "919374973952";
    $Text = urlencode("Edsystems Msg Sent : ".$totalSms);

    $ID          = "EDSystems";
    $Pwd         = "EDSystems";
    $baseurl     = "http://ip.muicsms.co.in/smsserver";
    //Invoke HTTP Submit url
    $url = "$baseurl/SMS10N.aspx?Userid=$ID&UserPassword=$Pwd&PhoneNumber=$PhNo&Text=$Text&GSM=EDSYSM";
    
    //Invoke HTTP Submit url
    //$url = "$baseurl/sms.aspx?Id=$ID&Pwd=$Pwd&PhNo=$PhNo&text=$Text";
    // do sendmsg call
    $ret = file($url);
    //Process $ret to check whether it contains "Message Submitted"
    //..............
    //..............
  }
  include("./bottom.php");
  $smarty->assign("msgGet",$msgGet);
  $smarty->assign("techArray",$techArray);
  //$smarty->assign("partyArray",$partyArray);
  $smarty->display("sendSMS.tpl");
}
?>